package com.ruoyi.note.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.note.domain.NoteDocument;
import com.ruoyi.note.mapper.NoteDocumentMapper;
import com.ruoyi.note.mapper.NoteAttachmentMapper;
import com.ruoyi.note.service.INoteDocumentService;

/**
 * 文档管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-22
 */
@Service
public class NoteDocumentServiceImpl implements INoteDocumentService 
{
    @Autowired
    private NoteDocumentMapper noteDocumentMapper;

    @Autowired
    private NoteAttachmentMapper noteAttachmentMapper;

    /**
     * 查询文档管理
     * 
     * @param documentId 文档管理主键
     * @return 文档管理
     */
    @Override
    public NoteDocument selectNoteDocumentByDocumentId(Long documentId)
    {
        NoteDocument noteDocument = noteDocumentMapper.selectNoteDocumentByDocumentId(documentId);
        if (noteDocument != null)
        {
            // 查询附件列表
            noteDocument.setAttachments(noteAttachmentMapper.selectAttachmentsByRelation("2", documentId));
        }
        return noteDocument;
    }

    /**
     * 查询文档管理列表
     * 
     * @param noteDocument 文档管理
     * @return 文档管理
     */
    @Override
    public List<NoteDocument> selectNoteDocumentList(NoteDocument noteDocument)
    {
        return noteDocumentMapper.selectNoteDocumentList(noteDocument);
    }

    /**
     * 新增文档管理
     * 
     * @param noteDocument 文档管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNoteDocument(NoteDocument noteDocument)
    {
        int rows = noteDocumentMapper.insertNoteDocument(noteDocument);
        insertNoteAttachment(noteDocument);
        return rows;
    }

    /**
     * 修改文档管理
     * 
     * @param noteDocument 文档管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNoteDocument(NoteDocument noteDocument)
    {
        // 删除原有附件
        noteAttachmentMapper.deleteAttachmentsByRelation("2", noteDocument.getDocumentId());
        // 新增附件
        insertNoteAttachment(noteDocument);
        return noteDocumentMapper.updateNoteDocument(noteDocument);
    }

    /**
     * 批量删除文档管理
     * 
     * @param documentIds 需要删除的文档管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNoteDocumentByDocumentIds(String documentIds)
    {
        Long[] documentIdArray = Convert.toLongArray(documentIds);
        // 删除附件
        for (Long documentId : documentIdArray)
        {
            noteAttachmentMapper.deleteAttachmentsByRelation("2", documentId);
        }
        return noteDocumentMapper.deleteNoteDocumentByDocumentIds(documentIdArray);
    }

    /**
     * 删除文档管理信息
     * 
     * @param documentId 文档管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNoteDocumentByDocumentId(Long documentId)
    {
        // 删除附件
        noteAttachmentMapper.deleteAttachmentsByRelation("2", documentId);
        return noteDocumentMapper.deleteNoteDocumentByDocumentId(documentId);
    }

    /**
     * 增加阅读次数
     * 
     * @param documentId 文档ID
     * @return 结果
     */
    @Override
    public int increaseViewCount(Long documentId)
    {
        return noteDocumentMapper.increaseViewCount(documentId);
    }

    /**
     * 新增文档附件信息
     * 
     * @param noteDocument 文档对象
     */
    public void insertNoteAttachment(NoteDocument noteDocument)
    {
        if (noteDocument.getAttachments() != null && noteDocument.getAttachments().size() > 0)
        {
            noteDocument.getAttachments().forEach(attachment -> {
                attachment.setRelationType("2");
                attachment.setRelationId(noteDocument.getDocumentId());
                noteAttachmentMapper.insertNoteAttachment(attachment);
            });
        }
    }
}